<template>
	<div class="bg">
		<el-card class="box-card">
			<div slot="header">
				<span>注册</span>
			</div>
			<el-form :model="form" :rules="rules" ref="register_form" status-icon label-width="80px">
				<el-form-item label="账号" prop="username">
					<el-input type="text" v-model="form.username" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input type="password" v-model="form.password" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="check">
					<el-input type="password" v-model="form.check" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="姓名" prop="fullname">
					<el-input type="text" v-model="form.fullname  " autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="手机" prop="tel">
					<el-input type="text" v-model="form.tel  " autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item prop="isAgree">
					<el-checkbox-group v-model="form.isAgree" autocomplete="off">
						<el-checkbox label="同意本站用户协议" name="type"></el-checkbox>
					</el-checkbox-group>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleRegister">注册</el-button>
				</el-form-item>
			</el-form>
			<div>
				<el-link href="/#/" type="info">登录账户</el-link>
				<router-link to="/" class="forget">忘记密码？</router-link>
			</div>
		</el-card>
	</div>
</template>

<script>
	import { Admin } from '@/api';
	export default {
		data() {
			var validate_password = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.form.password) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				form: {
					username: '',
					password: '',
					check: '',
					fullname: '',
					tel: '',
					isAgree: false,
				},
				rules: {
					username: [
						{ required: true, message: '请输入账号名称', trigger: 'blur' },
						{ min: 3, max: 5, message: '账号长度在 3 到 5 个字符', trigger: 'blur' }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' },
						{ pattern: /^\d{3,}/, message: '密码至少3个数字', trigger: 'blur' }
					],
					check: [{ validator: validate_password, trigger: 'blur' }],
					fullname: [
						{ required: true, message: '请输入姓名', trigger: 'blur' },
						{ min: 2, max: 40, message: '姓名长度在 2 到 40 个字符', trigger: 'blur' }
					],
					tel: [
						{ required: true, message: '请输入手机号码', trigger: 'blur' },
						{
							pattern: /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/,
							message: '手机号码不合规',
							trigger: 'blur'
						}
					],
					isAgree: [{ required: true, message: '请同意本站协议', trigger: 'blur' }]
				}
			}
		},
		created() {
			document.title = "注册---CMS管理系统"
		},
		methods: {
			handleRegister() {
				this.$refs.register_form.validate(async (valid) => {
					if (valid) {
						let { status, msg } = await Admin.register({ ...this.form });
						if (status) {
							//信息提示
							this.$message.success(msg);
							//跳转页面
							this.$router.push('/#/');
						} else {
							//信息提示
							this.$message.error(msg);
						}
					}
				});
			}
		}
	}
</script>

<style lang="less" scoped>
	.bg {
		width: 100vw;
		height: 100vh;
		background: url(./img/register-bg.jpg) center no-repeat;
		background-size: cover;
		position: relative;

		.box-card {
			position: fixed;
			right: 30px;
			top: 50%;
			transform: translateY(-50%);
			width: 400px;

			.forget {
				position: absolute;
				right: 20px;
			}
		}
	}
</style>
